/**
 * 岗位详细信息编辑面板
 */	
Ext.namespace("Ext.ux.sysmeta");

Ext.ux.sysmeta.SysColumnDetailPanel = Ext.extend(Ext.Panel, {

    title:'',
    layout:'fit',
    
    parentWinId: null,		//父窗口ID
    taleId: null,			//表ID
    columnId:null,			//字段ID
    tableName:null,
    formPanel: null,
    
	// 组件事件初始化
	initComponent: function(){
		Ext.ux.sysmeta.SysColumnDetailPanel.superclass.initComponent.call(this);
		
		// JSON解析
		var formReader = new Ext.data.JsonReader({
			root: 'result',
			id: 'id'
		},[
			{name:'id', mapping:'id'},
			{name:'tableId', mapping:'tableId'},	
			{name:'tableName', mapping:'tableName'},
			{name:'columnName', mapping:'columnName'},
			{name:'columnComments', mapping:'columnComments'},
			{name:'columnType', mapping:'columnType'},
			{name:'columnLength', mapping:'columnLength'},	
			{name:'columnPrecision', mapping:'columnPrecision'},
			{name:'columnScale', mapping:'columnScale'},
			{name:'nullable', mapping:'nullable'},
			{name:'isPrimaryKey', mapping:'isPrimaryKey'},
			{name:'propertyName', mapping:'propertyName'},
			{name:'memo', mapping:'memo'}
		]);		
		
		//岗位信息显示面板	
		var columnInfForm = new Ext.FormPanel({
			region: 'center',
			labelWidth: 85,	
			reader: formReader,
			labelAlign: 'right',
			frame: true,
			items: [{
				xtype: 'fieldset',
	            title: '<span class="fs_tip">请输入表结构基本信息，标志 <span class="fs_flag">*</span> 字段为必填项</span>',
	            layout: 'form',
				items:[{
					layout:'column',     //第1行
					items:[{
						columnWidth: 0.5,
						layout: 'form',
						items: [{
							xtype: 'textfield',
							fieldLabel: '表名',
							disabled: true,
							name: 'tableName',
							anchor: '96%'
						}]
					},{
						columnWidth: 0.5,
						layout: 'form',
						items: [{
							xtype: 'textfield',
							fieldLabel: '<span class="fs_flag">*</span>&nbsp;字段名',
							name: 'columnName',
							allowBlank: false,
							anchor: '96%'
						}]
					}]
				},{               
					layout:'column',     //第2行
					items:[{
						columnWidth: 0.5,
						layout: 'form',
						items: [{
							xtype: 'textfield',
							fieldLabel: '字段注释',           //只读
							name: 'columnComments',
							anchor: '96%'
						}]
					},{
						columnWidth: 0.5,
						layout: 'form',
						items: [{
							xtype: 'sysCodeCombo',
							fieldLabel: '<span class="fs_flag">*</span>&nbsp;字段类型',           //只读
							hiddenName: 'columnType',
							allowBlank: false,
							typeCode: 'AOS_COLUMN_TYPE',
							anchor: '96%'
						}]
					}]
				},{               
					layout:'column',     //第3行
					items:[{
						columnWidth: 0.5,
						layout: 'form',
						items: [{
							xtype: 'textfield',
							fieldLabel: '字段长度',           //只读
							name: 'columnLength',
							anchor: '96%'
						}]
					},{
						columnWidth: 0.5,
						layout: 'form',
						items: [{
							xtype: 'textfield',
							fieldLabel: '字段精度',           //只读
							name: 'columnPrecision',
							anchor: '96%'
						}]
					}]
				},{               
					layout:'column',     //第4行
					items:[{
						columnWidth: 0.5,
						layout: 'form',
						items: [{
							xtype: 'textfield',
							fieldLabel: '字段小数位',           //只读
							name: 'columnScale',
							anchor: '96%'
						}]
					},{
						columnWidth: 0.5,
						layout: 'form',
						items: [{
							xtype: 'sysCodeCombo',
							fieldLabel: '是否为空',           //只读
							hiddenName: 'nullable',
							typeCode: 'AOS_COLUMN_NULLABLE',
							anchor: '96%'
						}]
					}]
				},{               
					layout:'column',     //第5行
					items:[{
						columnWidth: 0.5,
						layout: 'form',
						items: [{
							xtype: 'sysCodeCombo',
							fieldLabel: '<span class="fs_flag">*</span>&nbsp;是否主键',           //只读
							hiddenName: 'isPrimaryKey',
							typeCode: 'AOS_COLUMN_ISPK',
							anchor: '96%'
						}]
					},{
						columnWidth: 0.5,
						layout: 'form',
						items: [{
							xtype: 'textfield',
							fieldLabel: '<span class="fs_flag">*</span>&nbsp;属性名',           //只读
							name: 'propertyName',
							allowBlank: false,
							anchor: '96%'
						}]
					}]
				},{                      //第6行
					layout: 'form',     
					xtype: 'textarea',
					name: 'memo',
					width: 180,
					anchor: '98%',
					fieldLabel: '备注',
					hight:70
				},{                     //隐藏控件
					xtype: 'hidden',
					name: 'id'
				}]
			}],
			buttons:[{
				xtype: 'button',
				text: '&nbsp;&nbsp;保存',
				width: 90,
				iconCls: 'icon-middle-save',
				scale: 'medium',
				handler: this.saveColumn,
				scope: this
			}]
	    });
		
		if(this.tableName != null){
			columnInfForm.form.findField('tableName').setValue(this.tableName);
		}
		
	    //如果是弹出窗口则添加关闭窗口按钮
	    if(this.parentWinId != null){
	    	columnInfForm.addButton({
				text: '关闭窗口',
				width: 90,
				iconCls: 'icon-middle-cancel',
				scale: 'medium',
				handler: function(button, e){
					this.closeParentWin();
				},
				scope: this
			})
	    }

	    this.add(columnInfForm);
	    this.formPanel = columnInfForm;
	},
	
	//加载指定岗位信息
	loadColumn: function(columnId){
		this.columnId = columnId;
		this.formPanel.load({
			url: 'sys-meta-column!input.action?id=' + columnId,
			waitMsg: '正在载入数据...',
			failure: function(){
				Ext.Msg.alert('系统提示', '无法加载指定岗位信息！');
			}
		});
	},
	
	//如果是弹出窗口则保存后要关闭
	closeParentWin: function(){
		var win = Ext.getCmp(this.parentWinId);
		if(win != null){
			win.close();
		}
	},
	
	//保存字段信息
	saveColumn: function(){
		if(this.formPanel.form.isValid()){
			
			//普通增加字段
			var url = 'sys-meta-column!save.action';
			
			var tableId = this.tableId;
			//在指定表下增加字段
			if(tableId != null){
				url = 'sys-meta-column!save.action?tableId=' + tableId;
			}
	    	this.formPanel.form.submit({
	    		url: url,
				success: function(from, action) {
					Ext.getCmp('treePanel').refresh();
					this.closeParentWin();
				},
				failure: function(form, action) {
					Ext.Msg.alert('系统提示', '更新字段信息失败！');
				},
				waitMsg: '正在保存数据，请稍候...',
				scope: this
	    	});
	    }
	}
});
	
